package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Seller;
import com.ssh.entity.SuperMarket;
import com.ssh.manager.SellerManager;
import com.ssh.manager.SuperMarketManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/seller/seller.jsp"),
@Result(name="findSellersByPage", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.jobNumber,page\\.pageData\\[\\d+\\]\\.post," +
"page\\.pageData\\[\\d+\\]\\.yibiao,page\\.pageData\\[\\d+\\]\\.kaoqin,page\\.pageData\\[\\d+\\]\\.zhishi,page\\.pageData\\[\\d+\\]\\.jiqiao," +
"page\\.pageData\\[\\d+\\]\\.createDate,page\\.pageData\\[\\d+\\]\\.superMarket\\.id,page\\.pageData\\[\\d+\\]\\.superMarket\\.name," +
"page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.id,page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.name"}),
//@Result(name="findAll", type="json", params={"root","sellers","excludeproperties","sellers\\[\\d+\\]\\.id,sellers\\[\\d+\\]\\.name"}),
@Result(name="findAll", type="json", params={"root","sellers"}),
@Result(name="updateSeller", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="deleteSeller", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class SellerAction extends ActionSupport{
	@Resource(name="superMarketManagerImpl")
	private SuperMarketManager superMarketManager;
	@Resource(name="sellerManagerImpl")
	private SellerManager sellerManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private Seller seller;
	private SuperMarket superMarket;
	private String isDesc;
	private List<Seller> sellers;
	private String ids;
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public List<Seller> getSellers() {
		return sellers;
	}
	public void setSellers(List<Seller> sellers) {
		this.sellers = sellers;
	}
	public SuperMarket getSuperMarket() {
		return superMarket;
	}
	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public Seller getSeller() {
		return seller;
	}
	public void setSeller(Seller seller) {
		this.seller = seller;
	}
	
	/**
	 * 前往JSP页面
	 * @return
	 */
	public String goSellerPage(){
		
		return "success";
	}
	
	public String findAll(){
		sellers = sellerManager.getAll();
		return "findAll";
	}
	
	/**
	 * 分页查找
	 * @return
	 */
	public String findSellersByPage(){
		page.setNumberPerPage(20);
		Map conditionMap = new HashMap();
		conditionMap.put("sellerName", seller.getName());
		conditionMap.put("sellerJobNumber", seller.getJobNumber());
		conditionMap.put("sellerPost", seller.getPost());
		conditionMap.put("superMarket", superMarket.getId());
		sellerManager.findSellersByPage(conditionMap, page, isDesc);
		return "findSellersByPage";
	}
	
	/**
	 * 添加
	 * @return
	 */
	public String saveSeller(){
		superMarket = superMarketManager.getSuperMarketById(superMarket.getId());
		seller.setCreateDate(new Timestamp(System.currentTimeMillis()));
		seller.setJiqiao(0);
		seller.setKaoqin(0);
		seller.setYibiao(0);
		seller.setZhishi(0);
		seller.setSuperMarket(superMarket);
		seller.setDel(0);
		sellerManager.saveOrUpdateSeller(seller);
		return "success";
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String updateSeller(){
		try {
			superMarket = superMarketManager.getSuperMarketById(superMarket.getId());
			
			Seller s = sellerManager.getSellerById(seller.getId());
			
			s.setJiqiao(seller.getJiqiao());
			s.setJobNumber(seller.getJobNumber());
			s.setKaoqin(seller.getKaoqin());
			s.setName(seller.getName());
			s.setPost(seller.getPost());
			s.setSuperMarket(superMarket);
			s.setYibiao(seller.getYibiao());
			s.setZhishi(seller.getZhishi());
			
			sellerManager.saveOrUpdateSeller(s);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("商品修改失败", LogName.API);
			e.printStackTrace();
		}
		return "updateSeller";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String deleteSeller(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					sellerManager.deleteSeller(new Long(d[i]));
				}
			}else{
				sellerManager.deleteSeller(seller.getId());
			}
			int _p = (int) page.getCurPageNum();
			
			page.setNumberPerPage(20);
			page.setCurPageNum(1);
			Map conditionMap = new HashMap();
			conditionMap.put("sellerName", seller.getName());
			conditionMap.put("sellerJobNumber", seller.getJobNumber());
			conditionMap.put("sellerPost", seller.getPost());
			conditionMap.put("superMarket", superMarket.getId());
			sellerManager.findSellersByPage(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "deleteSeller";
	}
}
